package cn.wolfcode.controller;

import cn.wolfcode.annotation.UserParam;
import cn.wolfcode.domain.Travel;
import cn.wolfcode.domain.UserInfo;
import cn.wolfcode.query.TravelQueryObject;
import cn.wolfcode.service.IDestinationService;
import cn.wolfcode.service.ITravelService;
import cn.wolfcode.util.JsonResult;
import cn.wolfcode.util.UMEditorUploader;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 区域控制器类
 * @Author: 伍家钰
 * @Date: 2020/8/10 10:40
 * @Version: 1.0
 */
@RestController
@RequestMapping("travels")
public class TravelController {
    @Resource
    private ITravelService travelService;


    /**
     * 分页查询
     *
     * @param qo
     * @return
     */
    @RequestMapping("query")
    public Object query(TravelQueryObject qo) {
        Page<Travel> page = travelService.query(qo);
        return JsonResult.success(page);
    }

    /**
     * 上传图片
     *
     * @param upfile
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception {
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"};
        up.setAllowFiles(fileType);
        //单位KB
        up.setMaxSize(10000);
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName() + "\", " +
                "\"size\": " + up.getSize()
                + ", \"state\": \"" + up.getState() + "\", \"type\": \"" + up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";
        result = result.replaceAll("\\\\", "\\\\");
        if (callback == null) {
            return result;
        } else {
            return "<script>" + callback + "(" + result + ")</script>";
        }
    }

    /**
     * 游记编辑和保存(写游记)
     *
     * @param travel
     * @param request
     * @return
     */
    @PostMapping("saveOrUpdate")
    public Object saveOrUpdate(Travel travel, HttpServletRequest request) {
        // 保存后需要返回一个id给前端页面跳转到指定的游记中
        travelService.saveOrUpdate(travel, request);
        return JsonResult.success(travel.getId());
    }

    //================== 测试用户参数解析器 ====================

    /**
     * 测试获取当前登入用户对象的信息,这样做得好处是,可以不用再用查询语句去单独获取登入用户的信息了
     *
     * @param userInfo
     * @return
     */
    @GetMapping("info")
    public Object infoTest(@UserParam UserInfo userInfo) {
        return JsonResult.success(userInfo);
    }

    /**
     * 上面的方法贴了@UserParam注解,优先解析那个标签的
     */
    @GetMapping("userInfo")
    public Object userInfoTest(UserInfo userInfo) {
        return JsonResult.success(userInfo);
    }
    //-------------------------------------------------------------------

    /**
     * 游记明细
     *
     * @param id
     * @return
     */
    @GetMapping("detail")
    public Object detail(String id) {
        // 保存后需要返回一个id给前端页面跳转到指定的游记中
        Travel travel = travelService.get(id);
        return JsonResult.success(travel);
    }
}
